#include "comLogManager.h"
#include "ProjectNaviMgr.h"
#include "ttcodeStateChart.h"


#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>


ProjectNaviMgr::ProjectNaviMgr( wxWindow* parent)
:PanelProjectNavi(parent)
{
	m_treeCtrlDocument->SetImageList(globalIiMgr->getList());
	m_treeCtrlTarget->SetImageList(globalIiMgr->getList());
}


void ProjectNaviMgr::reloadProject( void )
{
	this->m_treeCtrlDocument->DeleteAllItems();
	this->m_treeCtrlTarget->DeleteAllItems();
	this->createDocumentTree();
	this->createTargetTree();
}

void ProjectNaviMgr::createDocumentTree(void)
{
	TiXmlElement* root = globalDocMgr->getElement("TTCodeProject");
	if( NULL == root )
	{
		ttcode_log << "root=NULL" << endl;
		return;
	}
	wxTreeItemId top = this->m_treeCtrlDocument->AddRoot("root no display" );
	wxTreeItemId	docItem;
	docItem = createTree(this->m_treeCtrlDocument,top,root);
	TiXmlElement* doc = globalDocMgr->getElement("document");
	if( NULL == doc )
	{
		ttcode_log << "doc=NULL" << endl;
		return;
	}
	createAllSubTrees(this->m_treeCtrlDocument,docItem,doc);
}

void ProjectNaviMgr::createTargetTree(void)
{
	TiXmlElement* targets = globalDocMgr->getElement("targets");
	if( NULL == targets )
	{
		ttcode_log << "targets=NULL" << endl;
		return;
	}
	wxTreeItemId top = this->m_treeCtrlTarget->AddRoot("root no display" );
	wxTreeItemId	targetItem;
	targetItem = createTree(this->m_treeCtrlTarget,top,targets);
	createAllSubTrees(this->m_treeCtrlTarget,targetItem,targets);
}


wxTreeItemId ProjectNaviMgr::createTree( wxTreeCtrl *tree,const wxTreeItemId & parent,TiXmlElement* elem)
{
	wxTreeItemId ret;

	const char* name = elem->Attribute("name");
	const char* data = elem->Attribute("id");
	int img = -1;
	if( "TTCodeProject" == elem->ValueStr() )
	{
		img = globalIiMgr->getId("taotie");
	}
	else if( "filter" == elem->ValueStr() )
	{
		img = globalIiMgr->getId("folder");
	}
	else if( "file" == elem->ValueStr() )
	{
		string type("text");
		const char* lang = elem->Attribute("lang");
		const char* path = elem->Attribute("path");
		if( lang )
		{
			string strLang = boost::to_lower_copy(string(lang));
			type = globalFileType[strLang];
		}
		else if( path )
		{
			boost::filesystem::path fsPath(path);
			string strLang = boost::filesystem::extension(fsPath);
			boost::to_lower(strLang);
			type = globalFileType[strLang];
		}
		img = globalIiMgr->getId(type);
	}
	else if( "targets" == elem->ValueStr() )
	{
		name = "targets";
		img = globalIiMgr->getId("targets");
	}
	else if( "target" == elem->ValueStr() )
	{
		img = globalIiMgr->getId("targets");
		const char* type = elem->Attribute("type");
		if(type)
		{
			img = globalIiMgr->getId(string(type));
		}
	}
	else if( "compile_file" == elem->ValueStr() )
	{
		img = globalIiMgr->getId("folder");
	}
	else if( "source_item" == elem->ValueStr() )
	{
		const char *idFile = elem->GetText();
		if( idFile )
		{
			TiXmlElement* fileElem = globalDocMgr->getElementById(string(idFile));
			if( fileElem )
			{
				name = fileElem->Attribute("name");
				data = fileElem->Attribute("id");
				string type("text");
				const char* lang = fileElem->Attribute("lang");
				const char* path = fileElem->Attribute("path");
				if( lang )
				{
					string strLang = boost::to_lower_copy(string(lang));
					type = globalFileType[strLang];
				}
				else if( path )
				{
					boost::filesystem::path fsPath(path);
					string strLang = boost::filesystem::extension(fsPath);
					boost::to_lower(strLang);
					type = globalFileType[strLang];
				}
				img = globalIiMgr->getId(type);
			}
		}
	}
	else
	{
	}
	if( name && data && 0 <= img )
	{
		NaviTreeViewData *wx_data = new NaviTreeViewData(data);
		ret = tree->AppendItem(parent,name,img,-1,wx_data);
	}
	return ret;
}

void ProjectNaviMgr::createAllSubTrees( wxTreeCtrl *tree,const wxTreeItemId & parent,TiXmlElement* elem)
{
	wxTreeItemId sub = this->createTree( tree,parent,elem );
	TiXmlElement* item = elem->FirstChildElement();
	for( ; item ; item = item->NextSiblingElement() )
	{
		if(	sub.IsOk())
		{
			createAllSubTrees( tree,sub,item);
		}
		else
		{
			createAllSubTrees( tree,parent,item);
		}
	}
}





void ProjectNaviMgr::m_treeCtrlDocumentOnTreeBeginDrag( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeBeginLabelEdit( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeDeleteItem( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeEndDrag( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeEndLabelEdit( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeItemActivated( wxTreeEvent& event )
{
	const wxTreeItemId item = event.GetItem();
	wxTreeItemData* pData = m_treeCtrlDocument->GetItemData(item);
	NaviTreeViewData* pViewData = dynamic_cast<NaviTreeViewData*>(pData);
	TiXmlElement* itemElem = NULL;
	if( pViewData )
	{
		string id = pViewData->getData();
		itemElem = globalDocMgr->getElementById(id);
	}
	if( itemElem )
	{
		globalTextEdit->tryOpenFile(itemElem);
	}
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeItemRightClick( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnTreeSelChanged( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlDocumentOnUpdateUI( wxUpdateUIEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeBeginDrag( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeBeginLabelEdit( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeDeleteItem( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeEndDrag( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeEndLabelEdit( wxTreeEvent& event )
{
}


void ProjectNaviMgr::m_treeCtrlTargetOnTreeItemActivated( wxTreeEvent& event )
{
}


void ProjectNaviMgr::m_treeCtrlTargetOnTreeItemRightClick( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnTreeSelChanged( wxTreeEvent& event )
{
}
void ProjectNaviMgr::m_treeCtrlTargetOnUpdateUI( wxUpdateUIEvent& event )
{
}


